﻿using System;
using System.Collections;
using UnityEngine;

// Token: 0x02000304 RID: 772
public class SkinnedCollisionHelper : MonoBehaviour
{
	// Token: 0x060012E6 RID: 4838 RVA: 0x000997B4 File Offset: 0x000979B4
	public virtual void Start()
	{
		this.Init();
	}

	// Token: 0x060012E7 RID: 4839 RVA: 0x000997C0 File Offset: 0x000979C0
	public virtual bool Init()
	{
		if (this.IsInit)
		{
			return true;
		}
		this.skinnedMeshRenderer = base.GetComponent<SkinnedMeshRenderer>();
		this.meshCollider = base.GetComponent<MeshCollider>();
		if (this.meshCollider != null && this.skinnedMeshRenderer != null)
		{
			this.meshCalc = UnityEngine.Object.Instantiate<Mesh>(this.skinnedMeshRenderer.sharedMesh);
			this.meshCalc.name = this.skinnedMeshRenderer.sharedMesh.name + "_calc";
			this.meshCollider.sharedMesh = this.meshCalc;
			this.meshCalc.MarkDynamic();
			Vector3[] vertices = this.skinnedMeshRenderer.sharedMesh.vertices;
			Vector3[] normals = this.skinnedMeshRenderer.sharedMesh.normals;
			Matrix4x4[] bindposes = this.skinnedMeshRenderer.sharedMesh.bindposes;
			BoneWeight[] boneWeights = this.skinnedMeshRenderer.sharedMesh.boneWeights;
			this.nodeWeights = new global::SkinnedCollisionHelper.CWeightList[this.skinnedMeshRenderer.bones.Length];
			for (int i = 0; i < this.skinnedMeshRenderer.bones.Length; i++)
			{
				this.nodeWeights[i] = new global::SkinnedCollisionHelper.CWeightList();
				this.nodeWeights[i].transform = this.skinnedMeshRenderer.bones[i];
			}
			for (int j = 0; j < vertices.Length; j++)
			{
				BoneWeight boneWeight = boneWeights[j];
				if (boneWeight.weight0 != 0f)
				{
					Vector3 p = bindposes[boneWeight.boneIndex0].MultiplyPoint3x4(vertices[j]);
					Vector3 n = bindposes[boneWeight.boneIndex0].MultiplyPoint3x4(normals[j]);
					this.nodeWeights[boneWeight.boneIndex0].weights.Add(new global::SkinnedCollisionHelper.CVertexWeight(j, p, n, boneWeight.weight0));
				}
				if (boneWeight.weight1 != 0f)
				{
					Vector3 p2 = bindposes[boneWeight.boneIndex1].MultiplyPoint3x4(vertices[j]);
					Vector3 n2 = bindposes[boneWeight.boneIndex1].MultiplyPoint3x4(normals[j]);
					this.nodeWeights[boneWeight.boneIndex1].weights.Add(new global::SkinnedCollisionHelper.CVertexWeight(j, p2, n2, boneWeight.weight1));
				}
				if (boneWeight.weight2 != 0f)
				{
					Vector3 p3 = bindposes[boneWeight.boneIndex2].MultiplyPoint3x4(vertices[j]);
					Vector3 n3 = bindposes[boneWeight.boneIndex2].MultiplyPoint3x4(normals[j]);
					this.nodeWeights[boneWeight.boneIndex2].weights.Add(new global::SkinnedCollisionHelper.CVertexWeight(j, p3, n3, boneWeight.weight2));
				}
				if (boneWeight.weight3 != 0f)
				{
					Vector3 p4 = bindposes[boneWeight.boneIndex3].MultiplyPoint3x4(vertices[j]);
					Vector3 n4 = bindposes[boneWeight.boneIndex3].MultiplyPoint3x4(normals[j]);
					this.nodeWeights[boneWeight.boneIndex3].weights.Add(new global::SkinnedCollisionHelper.CVertexWeight(j, p4, n4, boneWeight.weight3));
				}
			}
			this.UpdateCollisionMesh(false);
			this.IsInit = true;
			return true;
		}
		return false;
	}

	// Token: 0x060012E8 RID: 4840 RVA: 0x00099B48 File Offset: 0x00097D48
	public virtual bool Release()
	{
		UnityEngine.Object.Destroy(this.meshCalc);
		return true;
	}

	// Token: 0x060012E9 RID: 4841 RVA: 0x00099B58 File Offset: 0x00097D58
	public virtual void UpdateCollisionMesh(bool _bRelease = true)
	{
		Vector3[] vertices = this.meshCalc.vertices;
		for (int i = 0; i < vertices.Length; i++)
		{
			vertices[i] = Vector3.zero;
		}
		foreach (global::SkinnedCollisionHelper.CWeightList cweightList in this.nodeWeights)
		{
			Matrix4x4 localToWorldMatrix = cweightList.transform.localToWorldMatrix;
			foreach (object obj in cweightList.weights)
			{
				global::SkinnedCollisionHelper.CVertexWeight cvertexWeight = (global::SkinnedCollisionHelper.CVertexWeight)obj;
				vertices[cvertexWeight.index] += localToWorldMatrix.MultiplyPoint3x4(cvertexWeight.localPosition) * cvertexWeight.weight;
			}
		}
		for (int k = 0; k < vertices.Length; k++)
		{
			vertices[k] = base.transform.InverseTransformPoint(vertices[k]);
		}
		this.meshCalc.vertices = vertices;
		this.meshCollider.enabled = false;
		this.meshCollider.enabled = true;
	}

	// Token: 0x060012EA RID: 4842 RVA: 0x00099CB8 File Offset: 0x00097EB8
	public virtual void Update()
	{
	}

	// Token: 0x060012EB RID: 4843 RVA: 0x00099CBC File Offset: 0x00097EBC
	public virtual void LateUpdate()
	{
		if (!this.IsInit)
		{
			return;
		}
		if (this.forceUpdate)
		{
			if (this.updateOncePerFrame)
			{
				this.forceUpdate = false;
			}
			this.UpdateCollisionMesh(true);
		}
	}

	// Token: 0x040018A8 RID: 6312
	public bool forceUpdate;

	// Token: 0x040018A9 RID: 6313
	public bool updateOncePerFrame = true;

	// Token: 0x040018AA RID: 6314
	public bool calcNormal = true;

	// Token: 0x040018AB RID: 6315
	protected bool IsInit;

	// Token: 0x040018AC RID: 6316
	protected global::SkinnedCollisionHelper.CWeightList[] nodeWeights;

	// Token: 0x040018AD RID: 6317
	protected SkinnedMeshRenderer skinnedMeshRenderer;

	// Token: 0x040018AE RID: 6318
	protected MeshCollider meshCollider;

	// Token: 0x040018AF RID: 6319
	protected Mesh meshCalc;

	// Token: 0x02000305 RID: 773
	private class CVertexWeight
	{
		// Token: 0x060012EC RID: 4844 RVA: 0x00099CF0 File Offset: 0x00097EF0
		public CVertexWeight(int i, Vector3 p, Vector3 n, float w)
		{
			this.index = i;
			this.localPosition = p;
			this.localNormal = n;
			this.weight = w;
		}

		// Token: 0x040018B0 RID: 6320
		public int index;

		// Token: 0x040018B1 RID: 6321
		public Vector3 localPosition;

		// Token: 0x040018B2 RID: 6322
		public Vector3 localNormal;

		// Token: 0x040018B3 RID: 6323
		public float weight;
	}

	// Token: 0x02000306 RID: 774
	public class CWeightList
	{
		// Token: 0x060012ED RID: 4845 RVA: 0x00099D18 File Offset: 0x00097F18
		public CWeightList()
		{
			this.weights = new ArrayList();
		}

		// Token: 0x040018B4 RID: 6324
		public Transform transform;

		// Token: 0x040018B5 RID: 6325
		public ArrayList weights;
	}
}
